<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心 - 商品商城</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <link href="/css/shop.css" rel="stylesheet">
    <style>
        .profile-container {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .profile-avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 4px solid #f8f9fa;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .profile-header {
            border-bottom: 1px solid #eee;
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        
        .profile-info {
            margin-bottom: 20px;
        }
        
        .profile-info h5 {
            color: #333;
            font-weight: 600;
            margin-bottom: 15px;
        }
        
        .info-item {
            display: flex;
            margin-bottom: 10px;
        }
        
        .info-label {
            width: 100px;
            font-weight: 500;
            color: #666;
        }
        
        .info-value {
            flex: 1;
            color: #333;
        }
        
        .nav-tabs .nav-link {
            color: #666;
            font-weight: 500;
            border: none;
            border-bottom: 2px solid transparent;
            border-radius: 0;
            padding: 10px 15px;
        }
        
        .nav-tabs .nav-link:hover {
            color: #e74a3b;
            border-bottom-color: #e74a3b;
        }
        
        .nav-tabs .nav-link.active {
            color: #e74a3b;
            background-color: transparent;
            border-bottom: 2px solid #e74a3b;
        }
        
        .order-item {
            border: 1px solid #eee;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            transition: all 0.3s ease;
        }
        
        .order-item:hover {
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        
        .order-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px dashed #eee;
        }
        
        .order-number {
            font-weight: 600;
            color: #333;
        }
        
        .order-status {
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .status-pending {
            background-color: #fff3cd;
            color: #856404;
        }
        
        .status-paid {
            background-color: #d1ecf1;
            color: #0c5460;
        }
        
        .status-shipped {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-delivered {
            background-color: #d1ecf1;
            color: #0c5460;
        }
        
        .status-cancelled {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        .order-product {
            display: flex;
            margin-bottom: 10px;
        }
        
        .order-product-image {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 4px;
            margin-right: 10px;
        }
        
        .order-product-info {
            flex: 1;
        }
        
        .order-product-name {
            font-weight: 500;
            color: #333;
            margin-bottom: 5px;
        }
        
        .order-product-price {
            color: #e74a3b;
            font-weight: 600;
        }
        
        .order-product-quantity {
            color: #666;
        }
        
        .order-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 15px;
            padding-top: 10px;
            border-top: 1px dashed #eee;
        }
        
        .order-total {
            font-weight: 600;
            color: #e74a3b;
        }
        
        .order-actions {
            display: flex;
            gap: 10px;
        }
        
        .empty-orders {
            text-align: center;
            padding: 40px 0;
        }
        
        .empty-orders i {
            font-size: 64px;
            color: #ddd;
            margin-bottom: 20px;
        }
        
        .address-item {
            border: 1px solid #eee;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            position: relative;
        }
        
        .address-default {
            position: absolute;
            top: 15px;
            right: 15px;
            background-color: #e74a3b;
            color: white;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
        }
        
        .address-info {
            margin-bottom: 10px;
        }
        
        .address-name {
            font-weight: 600;
            margin-right: 10px;
        }
        
        .address-phone {
            color: #666;
        }
        
        .address-detail {
            color: #666;
            margin-bottom: 5px;
        }
        
        .address-actions {
            display: flex;
            gap: 10px;
        }
        
        .form-control:focus {
            border-color: #e74a3b;
            box-shadow: 0 0 0 0.25rem rgba(231, 74, 59, 0.25);
        }
        
        .btn-primary {
            background-color: #e74a3b;
            border-color: #e74a3b;
        }
        
        .btn-primary:hover {
            background-color: #c0392b;
            border-color: #c0392b;
        }
        
        .btn-outline-primary {
            color: #e74a3b;
            border-color: #e74a3b;
        }
        
        .btn-outline-primary:hover {
            background-color: #e74a3b;
            border-color: #e74a3b;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm sticky-top">
        <div class="container">
            <a class="navbar-brand" href="/shop">
                <i class="bi bi-shop"></i> 商品商城
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/shop">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/products">所有商品</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/categories">商品分类</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/shop/promotions">促销商品</a>
                    </li>
                </ul>
                <form class="d-flex me-3" action="/shop/search" method="get">
                    <input class="form-control me-2" type="search" name="keyword" placeholder="搜索商品...">
                    <button class="btn btn-outline-primary" type="submit">
                        <i class="bi bi-search"></i>
                    </button>
                </form>
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i> 我的账户
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="/auth/login">登录</a></li>
                            <li><a class="dropdown-item" href="/auth/register">注册</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item active" href="/shop/profile">个人中心</a></li>
                            <li><a class="dropdown-item" href="/shop/orders">我的订单</a></li>
                            <li><a class="dropdown-item" href="/shop/cart">购物车</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 页面头部 -->
    <div class="page-header">
        <div class="container">
            <h1 class="fw-bold">个人中心</h1>
            <p class="lead mb-0">管理您的账户信息和订单</p>
        </div>
    </div>

    <div class="container mb-5">
        <div class="row">
            <!-- 左侧导航 -->
            <div class="col-lg-3 mb-4">
                <div class="profile-container">
                    <div class="profile-header text-center">
                        <img src="/images/avatar-placeholder.jpg" alt="用户头像" class="profile-avatar mb-3">
                        <h4 th:text="${user.username ?: '用户名'}">用户名</h4>
                        <p class="text-muted" th:text="${user.email ?: 'user@example.com'}">user@example.com</p>
                    </div>
                    
                    <div class="list-group list-group-flush">
                        <a href="#profile" class="list-group-item list-group-item-action active" data-bs-toggle="pill">
                            <i class="bi bi-person me-2"></i>个人信息
                        </a>
                        <a href="#orders" class="list-group-item list-group-item-action" data-bs-toggle="pill">
                            <i class="bi bi-bag-check me-2"></i>我的订单
                        </a>
                        <a href="#addresses" class="list-group-item list-group-item-action" data-bs-toggle="pill">
                            <i class="bi bi-geo-alt me-2"></i>收货地址
                        </a>
                        <a href="#security" class="list-group-item list-group-item-action" data-bs-toggle="pill">
                            <i class="bi bi-shield-lock me-2"></i>账户安全
                        </a>
                    </div>
                </div>
            </div>
            
            <!-- 右侧内容 -->
            <div class="col-lg-9">
                <div class="tab-content">
                    <!-- 个人信息 -->
                    <div class="tab-pane fade show active" id="profile">
                        <div class="profile-container">
                            <h5 class="mb-4">个人信息</h5>
                            
                            <form id="profileForm">
                                <div class="row mb-3">
                                    <label for="username" class="col-sm-3 col-form-label">用户名</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="username" th:value="${user.username ?: ''}" readonly>
                                    </div>
                                </div>
                                
                                <div class="row mb-3">
                                    <label for="email" class="col-sm-3 col-form-label">电子邮箱</label>
                                    <div class="col-sm-9">
                                        <input type="email" class="form-control" id="email" th:value="${user.email ?: ''}">
                                    </div>
                                </div>
                                
                                <div class="row mb-3">
                                    <label for="phone" class="col-sm-3 col-form-label">手机号码</label>
                                    <div class="col-sm-9">
                                        <input type="tel" class="form-control" id="phone" th:value="${user.phone ?: ''}">
                                    </div>
                                </div>
                                
                                <div class="row mb-3">
                                    <label for="realName" class="col-sm-3 col-form-label">真实姓名</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="realName" th:value="${user.realName ?: ''}">
                                    </div>
                                </div>
                                
                                <div class="row mb-3">
                                    <label for="gender" class="col-sm-3 col-form-label">性别</label>
                                    <div class="col-sm-9">
                                        <select class="form-select" id="gender">
                                            <option value="">请选择</option>
                                            <option value="male" th:selected="${user.gender == 'male'}">男</option>
                                            <option value="female" th:selected="${user.gender == 'female'}">女</option>
                                            <option value="other" th:selected="${user.gender == 'other'}">其他</option>
                                        </select>
                                    </div>
                                </div>
                                
                                <div class="row mb-3">
                                    <label for="birthday" class="col-sm-3 col-form-label">生日</label>
                                    <div class="col-sm-9">
                                        <input type="date" class="form-control" id="birthday" th:value="${user.birthday}">
                                    </div>
                                </div>
                                
                                <div class="row">
                                    <div class="col-sm-9 offset-sm-3">
                                        <button type="submit" class="btn btn-primary">保存修改</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    
                    <!-- 我的订单 -->
                    <div class="tab-pane fade" id="orders">
                        <div class="profile-container">
                            <h5 class="mb-4">我的订单</h5>
                            
                            <!-- 订单为空 -->
                            <div th:if="${#lists.isEmpty(orders)}" class="empty-orders">
                                <i class="bi bi-bag-x"></i>
                                <h4>您还没有订单</h4>
                                <p class="text-muted">快去选购心仪的商品吧！</p>
                                <a href="/shop" class="btn btn-primary">
                                    <i class="bi bi-arrow-left me-2"></i>去购物
                                </a>
                            </div>
                            
                            <!-- 订单列表 -->
                            <div th:if="${!#lists.isEmpty(orders)}">
                                <div th:each="order : ${orders}" class="order-item">
                                    <div class="order-header">
                                        <div class="order-number">订单号: <span th:text="${order.orderNumber}">订单号</span></div>
                                        <div class="order-status" 
                                             th:classappend="${order.status == 'pending' ? 'status-pending' : 
                                                            order.status == 'paid' ? 'status-paid' : 
                                                            order.status == 'shipped' ? 'status-shipped' : 
                                                            order.status == 'delivered' ? 'status-delivered' : 
                                                            'status-cancelled'}">
                                            <span th:text="${order.status == 'pending' ? '待付款' : 
                                                           order.status == 'paid' ? '已付款' : 
                                                           order.status == 'shipped' ? '已发货' : 
                                                           order.status == 'delivered' ? '已送达' : 
                                                           '已取消'}">状态</span>
                                        </div>
                                    </div>
                                    
                                    <div th:each="item : ${order.items}" class="order-product">
                                        <img th:src="${item.productImage ?: '/images/product-placeholder.jpg'}" 
                                             th:alt="${item.productName}"
                                             class="order-product-image">
                                        <div class="order-product-info">
                                            <div class="order-product-name" th:text="${item.productName}">商品名称</div>
                                            <div class="d-flex justify-content-between">
                                                <div class="order-product-price">¥<span th:text="${#numbers.formatDecimal(item.price, 1, 2)}">0.00</span></div>
                                                <div class="order-product-quantity">x<span th:text="${item.quantity}">1</span></div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="order-footer">
                                        <div class="order-total">共 <span th:text="${order.totalQuantity}">0</span> 件商品，合计: ¥<span th:text="${#numbers.formatDecimal(order.totalAmount, 1, 2)}">0.00</span></div>
                                        <div class="order-actions">
                                            <a th:href="@{/shop/orders/{id}(id=${order.id})}" class="btn btn-sm btn-outline-primary">查看详情</a>
                                            <button th:if="${order.status == 'pending'}" class="btn btn-sm btn-primary">立即付款</button>
                                            <button th:if="${order.status == 'delivered'}" class="btn btn-sm btn-outline-success">确认收货</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 收货地址 -->
                    <div class="tab-pane fade" id="addresses">
                        <div class="profile-container">
                            <div class="d-flex justify-content-between align-items-center mb-4">
                                <h5 class="mb-0">收货地址</h5>
                                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addAddressModal">
                                    <i class="bi bi-plus-circle me-1"></i>添加地址
                                </button>
                            </div>
                            
                            <!-- 地址为空 -->
                            <div th:if="${#lists.isEmpty(addresses)}" class="empty-orders">
                                <i class="bi bi-geo-alt"></i>
                                <h4>您还没有收货地址</h4>
                                <p class="text-muted">添加收货地址，方便购物</p>
                                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addAddressModal">
                                    <i class="bi bi-plus-circle me-1"></i>添加地址
                                </button>
                            </div>
                            
                            <!-- 地址列表 -->
                            <div th:if="${!#lists.isEmpty(addresses)}">
                                <div th:each="address : ${addresses}" class="address-item">
                                    <div th:if="${address.isDefault}" class="address-default">默认</div>
                                    <div class="address-info">
                                        <div>
                                            <span class="address-name" th:text="${address.name}">收货人</span>
                                            <span class="address-phone" th:text="${address.phone}">联系电话</span>
                                        </div>
                                        <div class="address-detail" th:text="${address.province + ' ' + address.city + ' ' + address.district + ' ' + address.detail}">详细地址</div>
                                    </div>
                                    <div class="address-actions">
                                        <button class="btn btn-sm btn-outline-primary">编辑</button>
                                        <button class="btn btn-sm btn-outline-danger">删除</button>
                                        <button th:if="${!address.isDefault}" class="btn btn-sm btn-outline-secondary">设为默认</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 账户安全 -->
                    <div class="tab-pane fade" id="security">
                        <div class="profile-container">
                            <h5 class="mb-4">账户安全</h5>
                            
                            <div class="list-group">
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-1">修改密码</h6>
                                            <p class="mb-0 text-muted">定期更换密码，保护账户安全</p>
                                        </div>
                                        <button class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#changePasswordModal">
                                            修改
                                        </button>
                                    </div>
                                </div>
                                
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-1">绑定手机</h6>
                                            <p class="mb-0 text-muted">绑定手机号，方便找回密码</p>
                                        </div>
                                        <button class="btn btn-sm btn-outline-primary">
                                            <span th:if="${user.phone}">更换</span>
                                            <span th:unless="${user.phone}">绑定</span>
                                        </button>
                                    </div>
                                </div>
                                
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-1">绑定邮箱</h6>
                                            <p class="mb-0 text-muted">绑定邮箱，接收订单通知</p>
                                        </div>
                                        <button class="btn btn-sm btn-outline-primary">
                                            <span th:if="${user.email}">更换</span>
                                            <span th:unless="${user.email}">绑定</span>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 添加地址模态框 -->
    <div class="modal fade" id="addAddressModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">添加收货地址</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="addAddressForm">
                        <div class="mb-3">
                            <label for="addressName" class="form-label">收货人</label>
                            <input type="text" class="form-control" id="addressName" required>
                        </div>
                        <div class="mb-3">
                            <label for="addressPhone" class="form-label">联系电话</label>
                            <input type="tel" class="form-control" id="addressPhone" required>
                        </div>
                        <div class="mb-3">
                            <label for="addressProvince" class="form-label">省份</label>
                            <select class="form-select" id="addressProvince" required>
                                <option value="">请选择省份</option>
                                <option value="北京">北京</option>
                                <option value="上海">上海</option>
                                <option value="天津">天津</option>
                                <option value="重庆">重庆</option>
                                <option value="河北">河北</option>
                                <option value="山西">山西</option>
                                <option value="内蒙古">内蒙古</option>
                                <option value="辽宁">辽宁</option>
                                <option value="吉林">吉林</option>
                                <option value="黑龙江">黑龙江</option>
                                <option value="江苏">江苏</option>
                                <option value="浙江">浙江</option>
                                <option value="安徽">安徽</option>
                                <option value="福建">福建</option>
                                <option value="江西">江西</option>
                                <option value="山东">山东</option>
                                <option value="河南">河南</option>
                                <option value="湖北">湖北</option>
                                <option value="湖南">湖南</option>
                                <option value="广东">广东</option>
                                <option value="广西">广西</option>
                                <option value="海南">海南</option>
                                <option value="四川">四川</option>
                                <option value="贵州">贵州</option>
                                <option value="云南">云南</option>
                                <option value="西藏">西藏</option>
                                <option value="陕西">陕西</option>
                                <option value="甘肃">甘肃</option>
                                <option value="青海">青海</option>
                                <option value="宁夏">宁夏</option>
                                <option value="新疆">新疆</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label for="addressCity" class="form-label">城市</label>
                            <input type="text" class="form-control" id="addressCity" required>
                        </div>
                        <div class="mb-3">
                            <label for="addressDistrict" class="form-label">区县</label>
                            <input type="text" class="form-control" id="addressDistrict" required>
                        </div>
                        <div class="mb-3">
                            <label for="addressDetail" class="form-label">详细地址</label>
                            <textarea class="form-control" id="addressDetail" rows="3" required></textarea>
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="isDefault">
                            <label class="form-check-label" for="isDefault">设为默认地址</label>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="saveAddressBtn">保存</button>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 修改密码模态框 -->
    <div class="modal fade" id="changePasswordModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">修改密码</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="changePasswordForm">
                        <div class="mb-3">
                            <label for="currentPassword" class="form-label">当前密码</label>
                            <input type="password" class="form-control" id="currentPassword" required>
                        </div>
                        <div class="mb-3">
                            <label for="newPassword" class="form-label">新密码</label>
                            <input type="password" class="form-control" id="newPassword" required>
                        </div>
                        <div class="mb-3">
                            <label for="confirmPassword" class="form-label">确认新密码</label>
                            <input type="password" class="form-control" id="confirmPassword" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="changePasswordBtn">修改</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="row">
                <div class="col-md-4">
                    <h5>商品商城</h5>
                    <p>提供优质商品，满足您的购物需求</p>
                </div>
                <div class="col-md-4">
                    <h5>快速链接</h5>
                    <ul>
                        <li><a href="/shop">首页</a></li>
                        <li><a href="/shop/products">所有商品</a></li>
                        <li><a href="/shop/categories">商品分类</a></li>
                        <li><a href="/shop/promotions">促销商品</a></li>
                    </ul>
                </div>
                <div class="col-md-4">
                    <h5>客户服务</h5>
                    <ul>
                        <li><a href="#">帮助中心</a></li>
                        <li><a href="#">退换货政策</a></li>
                        <li><a href="#">配送说明</a></li>
                        <li><a href="#">联系我们</a></li>
                    </ul>
                </div>
            </div>
            <div class="footer-bottom">
                <p>&copy; 2023 商品商城. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 通用的提示函数
            function showToast(message, type = 'info') {
                // 创建toast容器（如果不存在）
                let toastContainer = document.querySelector('.toast-container');
                if (!toastContainer) {
                    toastContainer = document.createElement('div');
                    toastContainer.className = 'toast-container position-fixed bottom-0 end-0 p-3';
                    toastContainer.style.zIndex = '11';
                    document.body.appendChild(toastContainer);
                }
                
                // 创建toast元素
                const toastId = 'toast-' + Date.now();
                const toastHtml = `
                    <div id="${toastId}" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
                        <div class="toast-header">
                            <i class="bi bi-${type === 'success' ? 'check-circle-fill text-success' : 
                                           type === 'danger' ? 'exclamation-triangle-fill text-danger' : 
                                           type === 'warning' ? 'exclamation-triangle-fill text-warning' : 
                                           'info-circle-fill text-info'} me-2"></i>
                            <strong class="me-auto">${type === 'success' ? '成功' : 
                                                 type === 'danger' ? '错误' : 
                                                 type === 'warning' ? '警告' : '提示'}</strong>
                            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
                        </div>
                        <div class="toast-body">
                            ${message}
                        </div>
                    </div>
                `;
                
                toastContainer.insertAdjacentHTML('beforeend', toastHtml);
                
                // 显示toast
                const toastElement = document.getElementById(toastId);
                const toast = new bootstrap.Toast(toastElement);
                toast.show();
                
                // 监听隐藏事件，从DOM中移除
                toastElement.addEventListener('hidden.bs.toast', function() {
                    toastElement.remove();
                });
            }
            
            // 全局函数，供其他地方使用
            window.showToast = showToast;
            
            // 个人信息表单提交
            const profileForm = document.getElementById('profileForm');
            if (profileForm) {
                profileForm.addEventListener('submit', function(e) {
                    e.preventDefault();
                    
                    // 收集表单数据
                    const formData = new FormData(profileForm);
                    const data = {};
                    formData.forEach((value, key) => {
                        data[key] = value;
                    });
                    
                    // 发送请求
                    fetch('/shop/profile/update', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(data)
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            showToast('个人信息更新成功', 'success');
                        } else {
                            showToast('更新失败: ' + data.message, 'danger');
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showToast('更新失败，请重试', 'danger');
                    });
                });
            }
            
            // 添加地址表单提交
            const saveAddressBtn = document.getElementById('saveAddressBtn');
            if (saveAddressBtn) {
                saveAddressBtn.addEventListener('click', function() {
                    const form = document.getElementById('addAddressForm');
                    if (!form.checkValidity()) {
                        form.reportValidity();
                        return;
                    }
                    
                    // 收集表单数据
                    const data = {
                        name: document.getElementById('addressName').value,
                        phone: document.getElementById('addressPhone').value,
                        province: document.getElementById('addressProvince').value,
                        city: document.getElementById('addressCity').value,
                        district: document.getElementById('addressDistrict').value,
                        detail: document.getElementById('addressDetail').value,
                        isDefault: document.getElementById('isDefault').checked
                    };
                    
                    // 发送请求
                    fetch('/shop/addresses/add', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify(data)
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            showToast('地址添加成功', 'success');
                            // 关闭模态框
                            const modal = bootstrap.Modal.getInstance(document.getElementById('addAddressModal'));
                            modal.hide();
                            // 重置表单
                            form.reset();
                            // 刷新页面
                            setTimeout(() => {
                                location.reload();
                            }, 1000);
                        } else {
                            showToast('添加失败: ' + data.message, 'danger');
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showToast('添加失败，请重试', 'danger');
                    });
                });
            }
            
            // 修改密码表单提交
            const changePasswordBtn = document.getElementById('changePasswordBtn');
            if (changePasswordBtn) {
                changePasswordBtn.addEventListener('click', function() {
                    const form = document.getElementById('changePasswordForm');
                    if (!form.checkValidity()) {
                        form.reportValidity();
                        return;
                    }
                    
                    const currentPassword = document.getElementById('currentPassword').value;
                    const newPassword = document.getElementById('newPassword').value;
                    const confirmPassword = document.getElementById('confirmPassword').value;
                    
                    // 验证新密码和确认密码是否一致
                    if (newPassword !== confirmPassword) {
                        showToast('新密码和确认密码不一致', 'warning');
                        return;
                    }
                    
                    // 发送请求
                    fetch('/shop/profile/changePassword', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            currentPassword: currentPassword,
                            newPassword: newPassword
                        })
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            showToast('密码修改成功', 'success');
                            // 关闭模态框
                            const modal = bootstrap.Modal.getInstance(document.getElementById('changePasswordModal'));
                            modal.hide();
                            // 重置表单
                            form.reset();
                        } else {
                            showToast('修改失败: ' + data.message, 'danger');
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showToast('修改失败，请重试', 'danger');
                    });
                });
            }
        });
    </script>
</body>
</html>